package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrOctetString;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;

public class ProxyTransCommandRequest extends AxdrType {
    private ByteBuffer dataCode = null;
    private PIID piid = null;
    private OAD oad = null;
    private COMDCB comdcb = null;
    private Unsigned16 recvIdleSec = null;
    private Unsigned16 recvIdleByte = null;
    private AxdrOctetString transCmd = null;

    public ProxyTransCommandRequest() {
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = ByteBuffer.wrap(dataCode);
    }

    public void setAll(PIID piid, OAD oad, COMDCB comdcb, Unsigned16 recvIdleSec, Unsigned16 recvIdleByte, AxdrOctetString transCmd) {
        this.piid = piid;
        this.oad = oad;
        this.comdcb = comdcb;
        this.recvIdleSec = recvIdleSec;
        this.recvIdleByte = recvIdleByte;
        this.transCmd = transCmd;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        this.piid = new PIID();
        this.piid.setNumBit(8);
        codeLength += this.piid.decode(input);
        this.oad = new OAD();
        codeLength += this.oad.decode(input);
        this.comdcb = new COMDCB();
        codeLength += this.comdcb.decode(input);
        this.recvIdleSec = new Unsigned16();
        this.recvIdleSec.setConst();
        codeLength += this.recvIdleSec.decode(input);
        this.recvIdleByte = new Unsigned16();
        this.recvIdleByte.setConst();
        codeLength += this.recvIdleByte.decode(input);
        this.transCmd = new AxdrOctetString();
        codeLength += this.transCmd.decode(input);
        return codeLength;
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        int codeLength;
        if (this.dataCode != null) {
            byte[] data = this.dataCode.array();
            codeLength = data.length;
            for (int i = data.length - 1; i >= 0; i--) {
                output.write(data[i]);
            }
        } else {
            if (this.transCmd != null && this.recvIdleByte != null && this.recvIdleSec != null
                    && this.comdcb != null && this.oad != null && this.piid != null) {
                codeLength = 0;
                codeLength += this.transCmd.encode(output);
                codeLength += this.recvIdleByte.encode(output);
                codeLength += this.recvIdleSec.encode(output);
                codeLength += this.comdcb.encode(output);
                codeLength += this.oad.encode(output);
                codeLength += this.piid.encode(output);
            } else {
                throw new RuntimeException("transCmd || recvIdleByte || recvIdleSec || comdcb || oad || piid is null");
            }
        }
        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = ByteBuffer.wrap(revOStream.getArray());
        this.dataCode.order(ByteOrder.LITTLE_ENDIAN);
    }

    @Override
    public String toString() {
        if (this.transCmd != null && this.recvIdleByte != null && this.recvIdleSec != null
                && this.comdcb != null && this.oad != null && this.piid != null) {
            return "sequence: {" + "piid: " + this.piid + ", oad: " + this.oad + ", comdcb: " + this.comdcb + ", recvidlesec: " + this.recvIdleSec + ", recvidlebyte: " + this.recvIdleByte + ", transcmd: " + this.transCmd + "}";
        } else {
            return "transCmd || recvIdleByte || recvIdleSec || comdcb || oad || piid is null";
        }
    }
}


